package com.zhu.controller.admin;

import com.zhu.base.http.RestResponse;
import com.zhu.domain.TableColumn;
import com.zhu.domain.User;
import com.zhu.domain.enums.PropTypeEnum;
import com.zhu.domain.enums.UserRoleEnum;
import com.zhu.model.EchartsVM;
import com.zhu.model.admin.GetAllUsersVM;
import com.zhu.model.admin.UserManageVM;
import com.zhu.model.admin.TableColumnVM;
import com.zhu.model.user.UserConditionSelectVM;
import com.zhu.model.user.UserSelectVM;
import com.zhu.service.TableColumnService;
import com.zhu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author JoyBoyZH
 * @date 2022/6/2
 * @return
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/gim/api/admin/manageUser")
@Api(tags = "管理员用户模块说明")
public class AdminUserController {
    private final ModelMapper modelMapper;

    private final UserService userService;

    private final TableColumnService tableColumnService;


    @Autowired
    public AdminUserController(ModelMapper modelMapper, UserService userService, TableColumnService tableColumnService) {
        this.modelMapper = modelMapper;
        this.userService = userService;
        this.tableColumnService = tableColumnService;
    }


    @PostMapping(value = "/selectUser/allUsers")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取所有用户接口说明")
    public RestResponse<Object> selectAllUsers(@RequestBody UserConditionSelectVM conditionModel) {
        List<User> users = userService.getUserByCondition(conditionModel.getUserId(), conditionModel.getUserName(), conditionModel.getRealName());
        if (users.isEmpty()) {
            return RestResponse.failure("不存在符合条件的用户");
        }
        List<UserSelectVM> userList = new ArrayList<>();
        for (User user : users) {
            if (Boolean.FALSE.equals(user.getIsWarehouseManage())) {
                UserSelectVM modelInfo = modelMapper.map(user, UserSelectVM.class);
                userList.add(modelInfo);
            }
        }
        GetAllUsersVM model = new GetAllUsersVM();
        List<TableColumn> tableColumn = tableColumnService.getList(PropTypeEnum.USER.getCode());
        List<TableColumnVM> tableColumnVMList = new ArrayList<>();
        for (TableColumn tc : tableColumn) {
            TableColumnVM tableColumnVM = new TableColumnVM();
            tableColumnVM.setProp(tc.getProp());
            tableColumnVM.setPropName(tc.getPropName());
            tableColumnVMList.add(tableColumnVM);
        }
        model.setTableColumnList(tableColumnVMList);
        model.setUserList(userList);
        model.setTotalNum(users.size() - 1);
        if (userList.isEmpty()) {
            return RestResponse.failure("不存在符合条件的用户");
        }
        return RestResponse.ok(model);
    }

    @GetMapping(value = "/selectUser/permissions")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("获取用户权限分布接口说明")
    public RestResponse<Object> selectUsersPermissions() {
        List<User> list = userService.getAllUsers();
        List<EchartsVM> model = new ArrayList<>();
        int userManage = 0;
        int goodsManage = 0;
        int inventoryManage = 0;
        int warehouseManage = 0;
        for (User user : list) {
            if (Boolean.TRUE.equals(user.getIsUserManage())) {
                userManage += 1;
            }
            if (Boolean.TRUE.equals(user.getIsGoodsManage())) {
                goodsManage += 1;
            }
            if (Boolean.TRUE.equals(user.getIsInventoryManage())) {
                inventoryManage += 1;
            }
            if (Boolean.TRUE.equals(user.getIsWarehouseManage())) {
                warehouseManage += 1;
            }
        }
        EchartsVM model1 = new EchartsVM();
        model1.setName("用户管理权限");
        model1.setValue(userManage);

        EchartsVM model2 = new EchartsVM();
        model2.setName("货品管理权限");
        model2.setValue(goodsManage);

        EchartsVM model3 = new EchartsVM();
        model3.setName("库存管理权限");
        model3.setValue(inventoryManage);

        EchartsVM model4 = new EchartsVM();
        model4.setName("仓库管理权限");
        model4.setValue(warehouseManage);

        model.add(model1);
        model.add(model2);
        model.add(model3);
        model.add(model4);

        return RestResponse.ok(model);
    }

    @GetMapping(value = "/deleteUser/{userId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除用户接口说明")
    public RestResponse<Object> deleteUser(@PathVariable Integer userId) {
        if (userService.deleteUserById(userId)) {
            return RestResponse.ok("删除成功");
        } else {
            return RestResponse.failure("删除失败");
        }
    }

    @PostMapping(value = "/updateUser/managePermission")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("更改用户的管理权限接口说明")
    public RestResponse<Object> updateUserManage(@RequestBody UserManageVM model) {
        User user = userService.getUserById(model.getUserId());
        user.setIsUserManage(model.getUserManage());
        user.setIsGoodsManage(model.getGoodsManage());
        user.setIsInventoryManage(model.getInventoryManage());
        if (user.getIsUserManage() || user.getIsGoodsManage() || user.getIsInventoryManage()) {
            user.setRole(UserRoleEnum.ADMIN);
        } else {
            user.setRole(UserRoleEnum.USER);
        }
        if (userService.updateUserById(user)) {
            return RestResponse.ok("修改权限成功");
        } else {
            return RestResponse.failure("更改管理权限失败");
        }
    }
}
